package com.ruifu.pls.service;

import com.ruifu.pls.entity.DocAsnHeader;
import com.ruifu.pls.entity.DocOrderHeader;
import com.ruifu.pls.model.GLCallInputParam;
import com.ruifu.pls.model.statusUpdate.GLAsnHerader;
import com.ruifu.pls.repository.GDocOrderHeaderRepository;
import com.ruifu.pls.repository.StatusUpdateRepository;
import com.ruifu.pls.utils.HttpUtils;
import com.ruifu.pls.utils.SighUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.io.IOException;

/**
 * ********************************************************
 * * @desc  ：
 * * @author  ${user}
 * * @date    ${date}
 * * @version v1.0
 * * ${tags}
 * *******************************************************
 */
@Service
public class StatusUpdateService {
    @Autowired
    GDocOrderHeaderRepository gDocOrderHeaderRepository;

    @Autowired
    StatusUpdateRepository updateRepository;//入库
    private org.slf4j.Logger logger = LoggerFactory.getLogger(DocAsnHeader.class);

    @Transactional
    public void GLinStatusUpdateIn() {
        DocAsnHeader docAsnHeader = updateRepository.findByAsnHeader();

        if (docAsnHeader != null) {
        String statusUpdate = "";
        if (docAsnHeader.getUserdefine5() == null || docAsnHeader.getUserdefine5().equals("")) {
            statusUpdate = "no";
        } else {
            statusUpdate = docAsnHeader.getUserdefine5();//具体哪个订单状态的标记
        }
       // System.out.print(statusUpdate+"-----------------");


            String status = docAsnHeader.getAsnstatus();
            String asnno = docAsnHeader.getAsnno();
            GLAsnHerader glAsnHeader = docAsnHeader.glAsnHerader;
            GLAsnHerader glReturnAsnInputRaram = glAsnHeader;
            GLCallInputParam glCallInputParam = new GLCallInputParam();
            glCallInputParam.setData(glAsnHeader);//设置请求数据
            glCallInputParam.setMd5Sigh(SighUtil.getMD5(SighUtil.signParam(glCallInputParam)));
            glCallInputParam.setMethod("g2matrix.supplydistribute.updateAllOrderStatus");
            if (status.equals("00")) {
                if (statusUpdate.equals(status)) {
                    //logger.info("正向入库接单完成" + asnno + "单已经传过");
                    status= "11";
                    updateRepository.updateStatus(asnno, status);
                } else {
                    logger.info(glCallInputParam.toString());
                    try {
                        String responsse = HttpUtils.doPost("http://demo.api.g2link.cn:8081/", "router", glCallInputParam.toString());
                        //System.out.println(responsse);
                        logger.info(responsse);
                        if (responsse.contains("\"code\":0")) {
                           double gg= Double.parseDouble(status);
                            updateRepository.updateStatus(asnno, status);
                        } else {
                            status="11";
                            updateRepository.updateStatus(asnno, status);
                            logger.info(responsse);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }/*else  if (status.equals("40") ){
                if (statusUpdate.equals(status)) {
                    status= "11";
                    updateRepository.updateStatus(asnno, status);
                } else {
                    logger.info(glCallInputParam.toString());
                    try {
                        String responsse = HttpUtils.doPost("http://demo.api.g2link.cn:8081/", "router", glCallInputParam.toString());
                        //System.out.println(responsse);
                        logger.info(responsse);
                        if (responsse.contains("\"code\":0")) {
                            updateRepository.updateStatus(asnno, status);
                        } else {
                            status="11";
                            updateRepository.updateStatus(asnno, status);
                            logger.info(responsse);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }*/else if (status.equals("90") ){
                if (statusUpdate.equals(status)) {
                    status= "11";
                    updateRepository.updateStatus(asnno, status);
                } else {
                    logger.info(glCallInputParam.toString());
                    try {
                        String responsse = HttpUtils.doPost("http://demo.api.g2link.cn:8081/", "router", glCallInputParam.toString());
                        //System.out.println(responsse);
                        logger.info(responsse);
                        if (responsse.contains("\"code\":0")) {
                            updateRepository.updateStatus(asnno, status);
                        } else {
                            status="11";
                            updateRepository.updateStatus(asnno, status);
                            logger.info(responsse);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    @Transactional
    public void GLinStatusUpdateOut() {//出库
        DocOrderHeader docOrderHeader= gDocOrderHeaderRepository.findByDocOrderHeaderTwo();
        if (docOrderHeader != null) {
        String orderno = docOrderHeader.getOrderno();
        String statusUpdate = "";
        if (docOrderHeader.getUserdefine5() == null || docOrderHeader.getUserdefine5().equals("")) {
            statusUpdate = "no";
        } else {
            statusUpdate = docOrderHeader.getUserdefine5();//具体哪个订单状态的标记
        }
        String status = docOrderHeader.getSostatus();

            GLAsnHerader glAsnHeader = docOrderHeader.glOrderHerader;
            GLAsnHerader glReturnAsnInputRaram = glAsnHeader;
            GLCallInputParam glCallInputParam = new GLCallInputParam();
            glCallInputParam.setData(glAsnHeader);//设置请求数据
            glCallInputParam.setMd5Sigh(SighUtil.getMD5(SighUtil.signParam(glCallInputParam)));
            glCallInputParam.setMethod("g2matrix.supplydistribute.updateAllOrderStatus");
            if (status.equals("00") ) {
                if (statusUpdate.equals(status)) {
                    status="11";
                    updateRepository.updateStatusOrder(orderno, status);
                } else {
                    logger.info(glCallInputParam.toString());
                    try {
                        String responsse = HttpUtils.doPost("http://demo.api.g2link.cn:8081/", "router", glCallInputParam.toString());
                        //System.out.println(responsse);
                        logger.info(responsse);
                        if (responsse.contains("\"code\":0")) {

                            updateRepository.updateStatusOrder(orderno, status);
                        } else {
                            status="11";
                            updateRepository.updateStatusOrder(orderno, status);
                            logger.info(responsse);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }else  if (status.equals("63") ){
                if (statusUpdate.equals(status)) {
                    status="11";
                    updateRepository.updateStatusOrder(orderno, status);
                } else {
                    logger.info(glCallInputParam.toString());
                    try {
                        String responsse = HttpUtils.doPost("http://demo.api.g2link.cn:8081/", "router", glCallInputParam.toString());
                        logger.info(responsse);
                        if (responsse.contains("\"code\":0")) {

                            updateRepository.updateStatusOrder(orderno, status);
                        } else {
                            status="11";
                            updateRepository.updateStatusOrder(orderno, status);
                            logger.info(responsse);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }else if (status.equals("90")){
                if (statusUpdate.equals(status)) {
                    status="11";
                    updateRepository.updateStatusOrder(orderno, status);
                } else {
                    logger.info(glCallInputParam.toString());
                    try {
                        String responsse = HttpUtils.doPost("http://demo.api.g2link.cn:8081/", "router", glCallInputParam.toString());
                        //System.out.println(responsse);
                        logger.info(responsse);
                        if (responsse.contains("\"code\":0")) {

                            updateRepository.updateStatusOrder(orderno, status);
                        } else {
                            status="11";
                            updateRepository.updateStatusOrder(orderno, status);
                            logger.info(responsse);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }


}
